#ifndef _MANAGE_IO_H_
#define _MANAGE_IO_H_

#include "config.h"

#define MAX_IO_NUM_AI   0u
#define MAX_IO_NUM_DI   10u
#define MAX_IO_NUM_PI   0u
#define MAX_IO_NUM_DO   4u
#define MAX_IO_NUM_RO   2u

#define PUL_DRV_OUTPUT		0xFFFFu
#define NORMAL_DRV_OUTPUT	0x0000u

#define DI_DIAG_TIME_MAX	10u
#define DI_DIAG_PULSE_US	(2000u)
#define DO_DIAG_STEP_MAX	8u

#define BF_DO_VAL_RO	0x0003u

#define BF_DO_VAL_DO34	0x0004u
#define BF_DO_VAL_DO44	0x0008u
#define BF_DO_VAL_DO54	0x0010u
#define BF_DO_VAL_DO64	0x0020u

#define BF_DO_T_DO4		0x0008u
#define BF_DO_T_DO5		0x0010u
#define BF_DO_T_DO6		0x0020u

typedef struct _diag_di_info{
	uint8_t	u8_fault_cnt_sc;
	uint8_t	u8_fault_confirm;
}diag_di_info_t;
#define	FAULT_DI_FILT_SC	5u
#define	BF_FAULT_DI_SC		(0x01u)

typedef struct _diag_do_info
{
	//uint8_t		u8_fault_cnt_wire;
	uint8_t		u8_fault_cnt_tp;  //故障计数
	uint8_t		u8_fault_confirm; //故障确认
	//uint16_t	u16_current;
	uint16_t	u16_voltage;      //采样到的电压值
}diag_do_info_t;
#define	DIAG_DO_IDX_54	2u
#define	DIAG_DO_IDX_34	0u
#define	DIAG_DO_IDX_44	1u
#define	DIAG_DO_IDX_64	3u
#define	FAULT_DO_FILT_TP	6u
#define	FAULT_DO_RECO_TP	3u
 #define	BF_FAULT_DO_TP		(0x01u)

typedef struct _diag_do_sta{
	uint8_t		u8_do_diag_sw;
	//uint8_t		u8_rev;     //保留
	uint8_t   res;     //保留
	tim_dat_t	st_stable_tim;
}diag_do_sta_t;
#define	ADC_DO_ASW_C12	0u
#define	ADC_DO_ASW_C1	1u
#define	ADC_DO_ASW_C2	2u
#define	ADC_DO_ASW_V44	3u
#define	ADC_DO_ASW_V64	4u
#define	ADC_DO_ASW_V54	5u
#define	ADC_DO_ASW_V34	6u
#define	ADC_DO_ASW_MAX	7u
#define	ADC_DO_STABLE_TIM	20u	/* 2ms, unit is 0.1ms */
#define	ADC_DO_PUL_TIM_MAX	400u
#define	ADC_DO_PUL_LOW_V	1298u	/* 15V */
#define	ADC_DO_LIMIT_LOW	200u	/* 2.4V */
#define	ADC_DO_LIMIT_HIGH	1386u	/* 16V */

typedef struct _mio_var{
	uint32_t	u32_di_val		;
	uint32_t	u32_do_val		;
	uint32_t	u32_do_pul		;
	uint8_t			u8_di_diag_src[MAX_IO_NUM_DI];//ʵ��û���õ�
	diag_do_sta_t	st_do_diag_sta;
	diag_di_info_t	st_diag_di_info[MAX_IO_NUM_DI];
	diag_do_info_t	st_diag_do_info[MAX_IO_NUM_DO];
}mio_var_t;

void mio_init(void);
void mio_cyc_run(const uint8_t u8lc_init_val, const uint8_t u8lc_incr_val);
void mio_get_di(const uint8_t u8lc_init_val, const uint8_t u8lc_incr_val, \
				uint16_t * u16p_di_dat);
void mio_set_do(const uint8_t u8lc_init_val, const uint8_t u8lc_incr_val, \
				uint16_t u16_do_dat, uint8_t u8_mcu_addr);
uint8_t mio_get_di_fault(void);

static void mio_init_val(void);
static void mio_diag(uint8_t u8_mcu_addr);
static void mio_diag_di(uint16_t u16_step, uint16_t u16_sta, uint8_t u8_mcu_addr);
static void mio_diag_do(uint16_t u16_step, uint16_t u16_sta, uint8_t u8_mcu_addr);
static void mio_do_wire(void);
static void mio_creat_pulse_out(uint16_t u16_do_dat);
static bool_t mio_do_pattern_out(uint32_t u32_pattern);
static void mio_do_pattern_fault(uint16_t u16_normal_val, bool_t b8_fault, \
							diag_do_info_t *stp_diag_info,\
							diag_do_info_t *stp_diag_info_bak);
#endif
